import { _decorator, Component, Node,instantiate, Prefab, Vec2,Vec3, UITransform } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('ZombiesLayer')
export class ZombiesLayer extends Component {
    @property(Prefab)
    zombiePrefab = null // 僵尸的预制体

    @property(Node)
    plantMap = null;

    spawnInterval: number = 5;  // 生成间隔时间（秒）

    start() {
        // 定时生成僵尸
        this.schedule(this.spawnZombie, this.spawnInterval);
    }

    update(deltaTime: number) {
        
    }
    
    getAllZombies(){
        return this.node.children
    }
    spawnZombie() {
        if (this.zombiePrefab ) {
            // 实例化僵尸
            const randomRow = ~~(Math.random() * 5);
            const node = this.plantMap.children[9 * randomRow]
            const y = node.worldPosition.y + node.getComponent(UITransform).height / 2;
            const x = 0
            
            this.createZombie(this.node.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(x, y, 0)))
        }
    }
    createZombie(position) {
        // 实例化僵尸
        let zombie = instantiate(this.zombiePrefab);
        // 设置位置
        position.x = 700;
        zombie.setPosition(position);
        // 添加到场景中
        this.node.addChild(zombie);
    }
}


